//
// Created by tacom on 2022/8/25.
//

#include "NumPyMatConvert.h"

cv::Mat NumPyMatConvert::Ny8UC1ToMat(pybind11::array_t<unsigned char> &input) {
    // python传递的信息不能是引用，否则读取不全
    if (input.ndim() != 2)
        throw std::runtime_error("1-channel image must be 2 dims");
    pybind11::buffer_info buf = input.request();
    cv::Mat mat(buf.shape[0], buf.shape[1], CV_8UC1, (unsigned char*)buf.ptr);

    return mat;
}

cv::Mat NumPyMatConvert::Ny8UC3ToMat(pybind11::array_t<unsigned char> &input) {
    if (input.ndim() != 3)
        throw std::runtime_error("3-channel image must be 3 dims");
    pybind11::buffer_info buf = input.request();
    cv::Mat mat(buf.shape[0], buf.shape[1], CV_8UC3, (unsigned char*)buf.ptr);

    return mat;
}

pybind11::array_t<unsigned char> NumPyMatConvert::Mat8UC1ToNumpy(cv::Mat &input) {
    pybind11::array_t<unsigned char> dst = pybind11::array_t<unsigned char>({
        input.rows, input.cols
    }, input.data);
    return dst;
}

pybind11::array_t<unsigned char> NumPyMatConvert::Mat8UC3ToNumpy(cv::Mat &input) {
    pybind11::array_t<unsigned char> dst = pybind11::array_t<unsigned char>({
        input.rows, input.cols, 3
    }, input.data);
    return dst;
}
